package gat.api.rest.background.tg;

import com.bitsun.core.common.exception.AppException;
import com.bitsun.core.common.persistence.Pager;
import com.bitsun.core.common.web.ResultDTO;
import gat.api.config.annotation.CheckPermissions;
import gat.application.tg.TopupPriceService;
import gat.dto.req.tg.TgBotReqParams;
import gat.dto.req.tg.TopupPriceReqDto;
import gat.dto.req.tg.TopupPriceReqParams;
import gat.dto.res.tg.TgBotResDto;
import gat.dto.res.tg.TopupPriceResDto;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 价格规格
 * @Author: yanshukeji-java
 * @Email: yanshukeji-java
 */
@Slf4j
@SuppressWarnings("unchecked")
@RestController
@RequestMapping("/topupPrice")
public class TopupPriceApiController {


    @Autowired
    private TopupPriceService topupPriceService;


    /**
     * 保存价格
     * @param reqDto
     * @return
     */
    @CheckPermissions(value = "topupPrice:save")
    @PostMapping(consumes = {"application/json"}, produces = {"application/json"})
    public ResultDTO<TopupPriceResDto> addTopupPrice(
        @ApiParam(value = "要保存的对象", required = true) @Valid @RequestBody TopupPriceReqDto reqDto) {
        TopupPriceResDto resDto = topupPriceService.save(reqDto);
        return ResultDTO.ok(resDto);
    }


    /**
     * 删除价格
     * @param ids
     * @return
     */
    @CheckPermissions(value = "topupPrice:deleted")
    @DeleteMapping(value = "/{ids}", produces = {"application/json"})
    public ResultDTO<Void> deleteTopupPrice(
        @ApiParam(value = "要删除的对象主键，多个对象主键可以用英文逗号隔开", required = true) @PathVariable("ids") String ids) {
        boolean bl = topupPriceService.deleteByIds(ids);
        return ResultDTO.ok();
    }

    /**
     * 根据id查询价格
     * @param id
     * @return
     */
    @CheckPermissions(value = "topupPrice:getOne")
    @GetMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<TopupPriceResDto> getOneTopupPrice(@ApiParam(value = "要查询的对象主键", required = true) @PathVariable("id") Long id) {
        TopupPriceResDto resDto = topupPriceService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    /**
     * 修改价格
     * @param id
     * @param reqDto
     * @return
     */
    @CheckPermissions(value = "topupPrice:update")
    @PatchMapping(value = "/{id}", produces = {"application/json"})
    public ResultDTO<TopupPriceResDto> patchUpdateTopupPrice(@ApiParam(value = "对象ID", required = true) @PathVariable("id") Long id,
        @ApiParam(value = "要修改的对象，对象属性有值的才更新,即null值不更新", required = true) @Valid @RequestBody TopupPriceReqDto reqDto) {
        if (reqDto.getType() != null) {
            throw new AppException("不允许修改价格类型");
        }
        topupPriceService.updateProps(id, reqDto);
        TopupPriceResDto resDto = topupPriceService.selectOne(id);
        return ResultDTO.ok(resDto);
    }

    // 分页

    /**
     * 分页查询
     * @param params
     * @return
     */
    @CheckPermissions(value = "topupPrice:queryPage")
    @GetMapping(produces = {"application/json"})
    public ResultDTO<Pager<TopupPriceResDto>> queryPage(TopupPriceReqParams params) {
        Pager<TopupPriceResDto> resDtoPager = topupPriceService.queryPage(params);
        return ResultDTO.ok(resDtoPager);
    }

}
